import axios, { ResDataType } from "./ajax";

const baseUrl = process.env.REACT_APP_API_URL + "/api/question";

type SearchOption = {
  keyword: string;
  isStar: boolean;
  isDeleted: boolean;
  page: number;
  pageSize: number;
};

// 查询问卷
export async function getQuestionService(id: string): Promise<ResDataType> {
  const url = `${baseUrl}/info/${id}`;
  const data = (await axios.get(url)) as ResDataType;
  return data;
}

// 创建问卷
export async function createQuestionService(): Promise<ResDataType> {
  const url = baseUrl;
  const data = (await axios.post(url)) as ResDataType;
  return data;
}

// 查询问卷列表
export async function getQuestionListService(
  params: Partial<SearchOption> = {}
): Promise<ResDataType> {
  const url = baseUrl;
  const data = (await axios.get(`${url}/list`, { params })) as ResDataType;
  return data;
}

// 更新问卷
export async function updateQuestionService(
  id: string,
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  params: { [key: string]: any }
): Promise<ResDataType> {
  const url = `${baseUrl}/edit/${id}`;
  const data = (await axios.patch(url, params)) as ResDataType;
  return data;
}

// 批量更新问卷
export async function updateQuestionListService(params: {
  ids: Array<string>;
  isDeleted: boolean;
}): Promise<ResDataType> {
  const url = `${baseUrl}/edit`;
  const data = (await axios.post(url, params)) as ResDataType;
  return data;
}

// 复制问卷
export async function duplicateQuestionService(
  id: string
): Promise<ResDataType> {
  const url = `${baseUrl}/duplicate/${id}`;
  const data = (await axios.post(url)) as ResDataType;
  return data;
}

// 彻底删除
export async function deleteQuestionsService(
  ids: string[]
): Promise<ResDataType> {
  const url = baseUrl;
  const data = (await axios.delete(url, {
    params: { ids: ids.join(",") },
  })) as ResDataType;
  return data;
}

// 获取统计列表
export async function getQuestionStatListService(
  questionId: string,
  opt: { page: number; pageSize: number }
): Promise<ResDataType> {
  const url = `${baseUrl}/stat/${questionId}`;
  const data = (await axios.get(url, { params: opt })) as ResDataType;
  return data;
}

// 获取单个组件统计信息
export async function getComponentStatService(
  questionId: string,
  componentId: string
): Promise<ResDataType> {
  const url = `${baseUrl}/stat/${questionId}/${componentId}`;
  const data = (await axios.get(url)) as ResDataType;
  return data;
}

// 获取使用统计信息
export async function getUsageStatService(): Promise<ResDataType> {
  const url = `${baseUrl}/usage`;
  const data = (await axios.get(url)) as ResDataType;
  return data;
}

// 删除答卷记录
export async function deleteAnswerService(
  questionId: string,
  answerId: string
): Promise<ResDataType> {
  const url = `${baseUrl}/stat/${questionId}/${answerId}`;
  const data = (await axios.delete(url)) as ResDataType;
  return data;
}
